package com.android.pagemarket.ui.activity.query;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.android.pagemarket.R;
import com.android.pagemarket.business.logic.InitManager;
import com.android.pagemarket.io.protocol.in.HttpAction.ActionListener;
import com.android.pagemarket.io.protocol.in.OrderDetailAction;
import com.android.pagemarket.io.protocol.out.Order;
import com.android.pagemarket.ui.activity.base.LoadableActivity;
import com.android.pagemarket.ui.widget.TitleBar;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-2
 * @see : 订单详情视图
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class OrderDetailActivity extends LoadableActivity
{

    private int mOid;
    
    private TextView num;
    private TextView order_data;
    private TextView give_data;
    private TextView paper;
    private TextView lb;
    private TextView paper_heigth;
    private TextView paper_width;
    private TextView paper_num;
    private TextView price;
    private TextView total;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if(!handIntent())
        {
            finish();
            return;
        }
        setContentView(R.layout.activity_orderdetail);
        initViews();
        load();
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar)
    {
        titleBar.setTitle(R.string.title_order_detail);
        titleBar.setRightVisible(false);
    }
    
    @Override
    public void refreshView()
    {
        load();
    }
    
    private void initViews()
    {
        num = getViewById(R.id.num);
        order_data = getViewById(R.id.order_data);
        give_data = getViewById(R.id.give_data);
        paper = getViewById(R.id.paper);
        lb = getViewById(R.id.lb);
        paper_heigth = getViewById(R.id.paper_heigth);
        paper_width = getViewById(R.id.paper_width);
        paper_num = getViewById(R.id.paper_num);
        price = getViewById(R.id.price);
        total = getViewById(R.id.total);
    }
    
    private void updataViews(Order order)
    {
        num.setText(order.oid+"");
        order_data.setText(order.orderDate);
        give_data.setText(order.finishDate);
        paper.setText(order.paper);
        lb.setText(order.lb);
        paper_heigth.setText(order.paperHeigth+"");
        paper_width.setText(order.paperWidth+"");
        price.setText( "" + order.price);
        total.setText( "" + String.format("%.2f", order.count * order.price));
        paper_num.setText("" + order.count);
        mLoadingView.showMainView();
    }
    
    private void load()
    {
        mLoadingView.showLoadingView();
        OrderDetailAction action = new OrderDetailAction(InitManager.getInstance().mLoginCompany.companyNum, mOid);
        action.setActionListener(new ActionListener<Order>()
        {
            @Override
            public void onSucceed(Order result)
            {
                if(result.isNoError())
                {
                    updataViews(result);
                }else
                {
                    mLoadingView.showLoadingErrView(result.resultInfo, false); 
                }
            }

            @Override
            public void onError(int resultCode)
            {
                mLoadingView.showLoadingErrView(OrderDetailAction.getErrorMean(resultCode), false); 
            }
        });
        action.submit();
    }
    
    private boolean handIntent()
    {
        Intent it = getIntent();
        mOid = it.getIntExtra(KEY_PARAM, 0);
        return mOid != 0;
    }
    
    public static void startActivity(Context context, int oid)
    {
        Intent it = new Intent(context, OrderDetailActivity.class);
        it.putExtra(KEY_PARAM, oid);
        context.startActivity(it);
    }
}
